#! /usr/bin/python 
# -*- coding: utf-8 -*-

import urllib2
import sys
import os
import time
import getopt

def FindModifiedFiles(rev1,rev2):
	# for each changeset, parse the file whether a bug is specified or not
	# if there is a bug issued there, download the xml file of the bug specification from bugzilla
	# if bug status is resolved and fixed, then parse changeset file again to gather filenames which are changed on this changeset and add them to the dictinoary to calculate number of change
	
	fileDictionary={}
	for i in range(rev1,rev2+1):
		filename= "%s%d" % ('changesetFiles/changeset_',i)
		f=open(filename, 'r')
		lines= f.readlines()
		f.close()

		fixedFlag=0  #if the bug is resolved and fixed 		
	
		for line in lines:
			if '+        https://bugs.webkit.org/show_bug' in line:
				print line
				try:
					bugID=int(line.split('=')[1])
				except ValueError:
					continue
				bugUrl="%s%d" % ('https://bugs.webkit.org/show_bug.cgi?ctype=xml&id=',bugID)
				bugXml=urllib2.urlopen(bugUrl).read()
			
				tempFileName="%s%d" % ('tempBugFile',bugID)
				output = open(tempFileName,'w')
				output.write(bugXml)
				output.close()
					
				f1=open(tempFileName, 'r')
				linesBug= f1.readlines()
				f1.close()
				flag=0
			
				for lineBug in linesBug:
					if '<bug_status>RESOLVED</bug_status>' in lineBug:
						flag=1
					if ('<resolution>FIXED</resolution>' in lineBug) and (flag == 1):
						fixedFlag=1
						break
				os.remove(tempFileName)
				break
	
		if fixedFlag == 1:
			for line in lines:
				if line.startswith('Index: '):
					#print line
					usefulpart=line.split(':')[1]
					if len(usefulpart.split('.')) > 1 :
						modifiedFileName=usefulpart.strip()					
						if modifiedFileName in fileDictionary:
							fileDictionary[modifiedFileName] += 1
						else:
							fileDictionary[modifiedFileName] = 1

	# write dictionary to a csv file.
	reportFileName = "%s_%s_%s%s" % ("report",rev1,rev2,".csv")
	report= open(reportFileName,'w')
	for filename,howmanytimes in fileDictionary.iteritems():
		print filename,howmanytimes
		report.write(filename)
		report.write(',%s' % howmanytimes)
		report.write('\n')
	report.close()

def DownloadChangeSetFiles(rev1,rev2):
	# for each changeset between start and end revision ids, download the diff files in the changesetFiles directory
	urlForChangeset='https://trac.webkit.org/changeset?format=diff&new='
	print "Downloading Changesets..."
	for i in range(rev1,rev2+1):
		filename = "%s%d" % ('changesetFiles/changeset_',i)
		if os.path.exists(filename):
			continue
		url= "%s%d" % (urlForChangeset,i)
		print url
		timeline1 = urllib2.urlopen(url)
		changesetContent=timeline1.read()
		output = open(filename,'w')
		output.write(changesetContent)
		output.close()

def GetRevisionNumber(dateUrl):
	# search timeline on trac and save its result for time
	timeline1 = urllib2.urlopen(dateUrl)
	timelineHtml1=timeline1.read()
	output = open('tempTimeFile1','w')
	output.write(timelineHtml1)
	output.close()

	# read line by line in html until changeset number for time is retrieved
	f1=open('tempTimeFile1', 'r')
	lines1= f1.readlines()
	f1.close()
	for line in lines1:
		if 'Changeset' in line:
			#print line
			temp=line.split('[')[1]
			revision=temp.split(']')[0]
			print "Revision: "+ revision
			break
	os.remove('tempTimeFile1')
	return revision

def Operate(date1,date2):
	timelineUrl1='https://trac.webkit.org/timeline?from='
	timelineUrl2='&daysback=5&changeset=on&update=Update'
	
	urlDate1= "%s%s%s" % (timelineUrl1,date1,timelineUrl2)  # url for time1 to determine start revision id
	urlDate2= "%s%s%s" % (timelineUrl1,date2,timelineUrl2)  # url for time2 to determine end revision id
	
	print urlDate1
	print urlDate2

	revisionNumStart=int(GetRevisionNumber(urlDate1))
	revisionNumEnd=int(GetRevisionNumber(urlDate2))
	DownloadChangeSetFiles(revisionNumStart,revisionNumEnd)
	FindModifiedFiles(revisionNumStart,revisionNumEnd)

def ShowHelp():
    print "Usage: trac.py --date1 [MM/dd/yy] --date2 [MM/dd/yy]"    
    sys.exit(0)


if __name__ == "__main__":
	date1 = None
	date2 = None
	try:
        	opts, args = getopt.getopt(sys.argv[1:],"d1:d2",["date1=","date2="])
        	for opt, arg in opts:
			if opt in ("-d1", "--date1"):
				date1 = arg
			elif opt in ("-d2", "--date2"):
				date2 = arg
		if len(sys.argv) == 1:
            		ShowHelp()
		elif not date1:
			print "Start date was not specified"
			ShowHelp()
		elif not date2:
			print "End date was not specified"
			ShowHelp()
		else:
			Operate(date1,date2)
	except getopt.GetoptError:
        	ShowHelp()

